#include "eqibwebpage.h"

EQIBWebPage::EQIBWebPage(QWebView *webView,EQIBControl *c) :
    QWebPage(c)
{
    this->webView = webView;
    this->c = c;
    QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QString location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    diskCache->setCacheDirectory(location);
    diskCache->clear();
    manager->setCache(diskCache);
    //setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
    setNetworkAccessManager(manager);
    settings()->setMaximumPagesInCache(20);
    this->setForwardUnsupportedContent(true);
    for(int i=0;i<67;i++)
        if(action((WebAction)i))action((WebAction)i)->setVisible(false);
    action(QWebPage::Back)->setVisible(true);
    action(QWebPage::Forward)->setVisible(true);
    action(QWebPage::Reload)->setVisible(true);
    action(QWebPage::Cut)->setVisible(true);
    action(QWebPage::Copy)->setVisible(true);
    action(QWebPage::Paste)->setVisible(true);
    action(QWebPage::SelectAll)->setVisible(true);
    action(QWebPage::Undo)->setVisible(true);
    action(QWebPage::Redo)->setVisible(true);
    //connect(this, SIGNAL(printRequested(QWebFrame*)),
    //        this, SLOT(print(QWebFrame*)));



    connect(this,SIGNAL(windowCloseRequested()),this,SLOT(on_webview_closeWindowRequest()));
    connect(this,SIGNAL(downloadRequested(QNetworkRequest)),this,SLOT(downloadRequested(QNetworkRequest)));
    connect(this,SIGNAL(unsupportedContent(QNetworkReply*)),this,SLOT(unsupportedContent(QNetworkReply*)));
    connect(this,SIGNAL(loadProgress(int)),this,SLOT(on_webView_loadProgress(int)));
    connect(this,SIGNAL(loadStarted()),this,SLOT(on_webView_loadStarted()));
    connect(this,SIGNAL(loadFinished(bool)),this,SLOT(on_webView_loadFinished(bool)));
    connect(this,SIGNAL(statusBarMessage(QString)),this,SLOT(on_webView_statusBarMessage(QString)));
    connect(this->webView,SIGNAL(titleChanged(QString)),this,SLOT(on_webView_titleChanged(QString)));
    connect(this->webView,SIGNAL(iconChanged()),this,SLOT(on_webView_iconChanged()));

    loadingTimer = new QTimer();
    this->loadingIconIndex = 0;
    connect(loadingTimer,SIGNAL(timeout()),this,SLOT(onLoadingTimerTimeout()));
}

EQIBWebPage::~EQIBWebPage(){
    delete this->loadingTimer;
}

QWebPage * EQIBWebPage::createWindow(WebWindowType type )
{
    Q_UNUSED(type);
    qDebug() << "on create new window";

    //QWebBrowser *view=new QWebBrowser(0,false);
    //view->resize(800,600);
    //view->show();
    //return view->webPage;
    return this->c->createPage();
}

void EQIBWebPage::on_webview_closeWindowRequest(){
    this->c->removeTab(this->findTabIndex());
    delete this;
}


void EQIBWebPage::downloadRequested(QNetworkRequest request){
    webView->stop();
    //ui->statusBar->showMessage("载入完成.");
    //new FileDownDialog(this,webPage->networkAccessManager()->get(request));
}


void EQIBWebPage::unsupportedContent(QNetworkReply *reply){
    webView->stop();
    //ui->statusBar->showMessage(tr("载入完成."));
    //new FileDownDialog(this,reply);
}

int EQIBWebPage::findTabIndex(){
    return this->c->indexOf(this->webView);
}

void EQIBWebPage::on_printAction_triggered()
{
    //webPage->printSetDialog();
}

void EQIBWebPage::on_webView_statusBarMessage(const QString &text)
{

}


void EQIBWebPage::contextMenuEvent(QContextMenuEvent * event ){
    QMenu * menu = new QMenu();
    //menu->addAction(ui->printAction);
    menu->exec(event->globalPos());
    delete menu;
}



void EQIBWebPage::on_webView_titleChanged(const QString &title)
{
    //if(title.length()!=0)
        //this->setWindowTitle(title);
    this->c->setTabText(this->findTabIndex(),title);
}

void EQIBWebPage::on_webView_customContextMenuRequested(const QPoint &pos)
{
    qDebug("..........");
}

void EQIBWebPage::onLoadingTimerTimeout(){
    if(this->loadingIconIndex!=-1){
        QIcon * loadingIcon = EQIBUtils::get()->loadingIcons->value(this->loadingIconIndex);
        //qDebug() << "loadingIcon" << this->loadingIconIndex << loadingIcon;
        this->c->setTabIcon(this->findTabIndex(), *loadingIcon);
        this->loadingIconIndex++;
        if(this->loadingIconIndex>=EQIBUtils::get()->loadingIcons->count())
            this->loadingIconIndex = 0;
        loadingTimer->start(100);
    }else{
        this->c->setTabIcon(this->findTabIndex(),this->webView->icon());
    }
}

void EQIBWebPage::on_webView_iconChanged()
{
    if(this->loadingIconIndex == -1)
        this->c->setTabIcon(this->findTabIndex(),this->webView->icon());
}

void EQIBWebPage::on_webView_loadFinished(bool flag)
{
    if(!flag){
    //    this->setWindowTitle(tr("加载失败"));
    //    ui->webView->setContent(QByteArray(tr("<br/><br/><br/><center>").append(ui->urlAddres->text()).append(tr("<br/><h1>内容加载失败，请重试！</h1></center>")).toUtf8()));
    }

   // ui->statusBar->showMessage("载入完成.");
    loadingIconIndex=-1;
}

void EQIBWebPage::on_webView_loadStarted()
{
    loadingIconIndex = 0;
    loadingTimer->start(500);
    //ui->statusBar->showMessage("开始载入...");
}

void EQIBWebPage::on_webView_loadProgress(int progress)
{
   // ui->statusBar->showMessage(QString("载入已完成").append(QString::number(progress)).append("%."));

}


